package com.neu.questionnaireplus.utils.common;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Base64;

@Service
@Slf4j
public class CommonUtils {

  static MessageDigest md5 = null;
  static Base64.Encoder base64en = Base64.getEncoder();

  static {
    try {
      md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      log.error("md5 init error", e);
    }
  }

  /** 
  * @Description: md5算法编码加密 
  * @Param: [sourceString] 
  * @return: java.lang.String 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public String encodeByMd5(String sourceString) throws UnsupportedEncodingException {
    return base64en.encodeToString(md5.digest(sourceString.getBytes("utf-8")));
  }


  static SimpleDateFormat sdfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  /** 
  * @Description: 格式化获取当前精确时间 
  * @Param: [] 
  * @return: java.lang.String 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public static String getFormatDateTimeNow() {
    return sdfFull.format(System.currentTimeMillis());

  }
/** 
* @Description: 格式化获取当前日期时间 
* @Param: [date] 
* @return: java.lang.String 
* @Author: ~FHX~ (:TSunS:)
* @Date: 2022/10/26 
*/ 
  public String getFormatDateTime(Date date) {
    return sdfFull.format(date);
  }


  /** 
  * @Description: 格式化时间戳 
  * @Param: [timeStamp] 
  * @return: java.lang.String 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public String getFormatDateTimeByTimeStamp(long timeStamp) {
    return sdfFull.format(timeStamp);
  }

  static SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");

  /** 
  * @Description: 将年月日字符串转成Date 
  * @Param: [dateString] 
  * @return: java.util.Date 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public static Date getDateByDateString(String dateString) {
    if (dateString == null) {
      return null;
    }
    Date date;
    try {
      date = sdfDate.parse(dateString);
    } catch (ParseException e) {
      date = null;
    }
    return date;
  }

  /** 
  * @Description: 将日期转成字符串 
  * @Param: [date] 
  * @return: java.lang.String 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public String getDateStringByDate(Date date) throws ParseException {
    if (date == null) {
      return null;
    }
    return sdfDate.format(date);
  }

  public Long getLongByDate(Date date) {
    if (date == null) {
      return null;
    }
    return date.getTime();
  }

/** 
* @Description: 生成随机的UUID 
* @Param: [] 
* @return: java.lang.String 
* @Author: ~FHX~ (:TSunS:)
* @Date: 2022/10/26 
*/ 
  public static String getUUID() {
    return UUID.randomUUID().toString().replace("-", "");
  }

 /** 
 * @Description: 计算两个日期相隔天数 
 * @Param: [before, then] 
 * @return: int 
 * @Author: ~FHX~ (:TSunS:)
 * @Date: 2022/10/26 
 */ 
  public int getDifferenceDay(Date before, Date then) {
    Calendar last_c = Calendar.getInstance();
    last_c.setTime(before);
    Calendar now_c = Calendar.getInstance();
    now_c.setTime(then);
    int last_year = last_c.get(Calendar.YEAR);
    int now_year = now_c.get(Calendar.YEAR);
    if (last_year == now_year) {//the same year
      return now_c.get(Calendar.DAY_OF_YEAR) - last_c.get(Calendar.DAY_OF_YEAR);
    } else {
      //different year
      long length = then.getTime() - before.getTime();
      return (int) length / (1000 * 3600 * 24);
    }
  }

  /** 
  * @Description: 通过request获取IP地址
  * @Param: [request] 
  * @return: java.lang.String 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public String getIPAddr(HttpServletRequest request) {
    String ip = request.getHeader("X-real-ip");//先从nginx自定义配置获取
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("x-forwarded-for");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    return ip;
  }
}

